/**
 * @program: yygh_parent
 * @ClassName HospitalServiceImpl
 * @description:
 * @author: zyw
 * @create: 2023-05-24 14:47
 * @Version 1.0
 **/
package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.hosp.mapper.HospitalSetMapper;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private HospitalSetMapper hospitalSetMapper;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        String s = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(s, Hospital.class);
        Hospital targetHospital = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        if (targetHospital == null) {
            //0：未上线  1：已上线
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
        } else {
            hospital.setStatus(targetHospital.getStatus());
            hospital.setCreateTime(targetHospital.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setId(targetHospital.getId());
            hospital.setIsDeleted(targetHospital.getIsDeleted());
        }
        hospitalRepository.save(hospital);
    }

    @Override
    public String getSignKeyWithHoscode(String hoscode) {
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        wrapper.eq("hoscode", hoscode);
        HospitalSet hospitalSet = hospitalSetMapper.selectOne(wrapper);
        if (hospitalSet == null) {
            throw new YyghException(20001, "该医院信息不存在");
        }
        return hospitalSet.getSignKey();
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    @Override
    public Page<Hospital> getHospitalPage(Integer pageNum, Integer pageSize, HospitalQueryVo hospitalQueryVo) {
        Hospital hospital = new Hospital();
        if (!StringUtils.isEmpty(hospitalQueryVo.getHosname())) {
            hospital.setHosname(hospitalQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hospitalQueryVo.getProvinceCode())) {
            hospital.setProvinceCode(hospitalQueryVo.getProvinceCode());
        }
        if (!StringUtils.isEmpty(hospitalQueryVo.getCityCode())) {
            hospital.setCityCode(hospitalQueryVo.getCityCode());
        }
        if (!StringUtils.isEmpty(hospitalQueryVo.getHostype())) {
            hospital.setCityCode(hospitalQueryVo.getHostype());
        }
        if (!StringUtils.isEmpty(hospitalQueryVo.getDistrictCode())) {
            hospital.setCityCode(hospitalQueryVo.getDistrictCode());
        }
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withMatcher("hosname", ExampleMatcher.GenericPropertyMatchers.contains())  //name字段模糊匹配
//                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        Example<Hospital> example = Example.of(hospital,matcher);

        Pageable pageRequest = PageRequest.of(pageNum - 1, pageSize, Sort.Direction.DESC, "createTime");
        Page<Hospital> page = hospitalRepository.findAll(example, pageRequest);
        page.getContent()
                .parallelStream()
                .forEach(item->{
//                    String hostype = item.getHostype();
//                    String provinceCode = item.getProvinceCode();
//                    String cityCode = item.getCityCode();
//                    String districtCode = item.getDistrictCode();
//
//                    String provinceAddress = dictFeignClient.getNameByValue(Long.parseLong(provinceCode));
//                    String cityAddress = dictFeignClient.getNameByValue(Long.parseLong(cityCode));
//                    String districtAddress = dictFeignClient.getNameByValue(Long.parseLong(districtCode));
//                    String level = dictFeignClient.getNameByValueAndDictCode(Long.parseLong(hostype), DictEnum.HOSTYPE.getDictCode());
//                    item.getParam().put("level", level);
//                    item.getParam().put("address", provinceAddress + cityAddress + districtAddress + hospital.getAddress());
                    this.packageHospital(item);
                });
        return page;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        if (status == 0 || status == 1) {
            Hospital hospital = hospitalRepository.findById(id).get();
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital detail(String id) {
        Hospital hospital = hospitalRepository.findById(id).get();
        this.packageHospital(hospital);
        return hospital;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public Hospital getHospitalByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        this.packageHospital(hospital);
        return hospital;
    }

    private void packageHospital(Hospital hospital){
        String hostype = hospital.getHostype();

        String provinceCode = hospital.getProvinceCode();
        String cityCode = hospital.getCityCode();
        String districtCode = hospital.getDistrictCode();


        String provinceAddress = dictFeignClient.getNameByValue(Long.parseLong(provinceCode));
        String cityAddress = dictFeignClient.getNameByValue(Long.parseLong(cityCode));
        String districtAddress = dictFeignClient.getNameByValue(Long.parseLong(districtCode));

        String level = dictFeignClient.getNameByValueAndDictCode(Long.parseLong(hostype), DictEnum.HOSTYPE.getDictCode());

        hospital.getParam().put("level", level);
        hospital.getParam().put("address", provinceAddress+cityAddress+districtAddress + hospital.getAddress());

    }

}
 
